﻿
@{
    Layout = "~/Views/Shared/_Index.cshtml";
}

<!DOCTYPE html>
<style>
    .showbotton {
        display: none;
        position: fixed;
        bottom: 5px;
        right: 0;
        z-index: 9999;
        background-color: #ccc;
        width: 50px;
        height: 44px;
        filter: alpha(Opacity=90);
        -moz-opacity: 0.9;
        opacity: 0.9;
        cursor: pointer;
        border-radius: 100px 0 0 100px;
    }

    #TabGrid {
        width: 100%;
        position: fixed;
        bottom: 10px;
        right: 0;
        z-index: 8888;
        background-color: #253443;
        filter: alpha(Opacity=92);
        -moz-opacity: 0.92;
        opacity: 0.92;
        box-shadow: 0px 2px 10px #909090;
    }

        #TabGrid > * {
            filter: alpha(Opacity=92);
            -moz-opacity: 0.92;
            opacity: 0.92;
        }
</style>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>PrepareMedicineApply</title>
</head>
<body>

    <form>
        <div class="panel panel-default">
            <table class="form">
                <tr>
                    <th class="formTitle" style="width:70px;padding-right:12px;">状态：</th>
                    <td class="formValue">
                        @*@Html.DropDownList("ksbyzt", EnumKSBYZT.sq.ToDescSelectList(), new { @class = "form-control form-an" })*@
                        <select id="ksbyzt" name="ksbyzt" class="form-control">
                            @*<option value="0">全部</option>
                            <option value="1">保存</option>
                            <option value="3">提交</option>
                            <option value="5">撤回</option>
                            <option value="7">作废</option>*@

                            <option value="0">全部</option>
                            <option value="1">保存</option>
                            <option value="2">已申请</option>
                            <option value="3">已确认</option>
                            <option value="4">撤回</option>
                            <option value="5">退回</option>
                            <option value="6">作废</option>
                        </select>
                    </td>
                    <th class="formTitle">日期：</th>
                    <td class="formValue" colspan="3">
                        <input id="kssj" type="text" class="form-control input-wdatepicker" style="width:42%; float:left;" value="@DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                        <span style="margin-left:1%;float:left">—</span>
                        <input id="jssj" type="text" class="form-control input-wdatepicker" style="width:43%; float:left;margin-left:1%;" value="@DateTime.Now.ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                    </td>
                    <td class="formValue title">
                        <input type="button" id="btn_search" class="btn btn-primary" style="width:36%;margin-left: 10%;" value="查询" />
                    </td>

                </tr>

            </table>
        </div>
        <div class="gridPanel">
            <table id="gridList"></table>
            <div id="gridPager"></div>
        </div>
        <div id="TabGrid">
            <ul class="nav nav-tabs navb-bg" role="tablist" id="myTab">
                <li role="presentation" class="active">
                    <a href="#dv_djmx" id="linkSearch" role="tab" data-toggle="tab">单据明细</a>
                </li>
                <li style="float:right">
                    <span class="hiding glyphicon glyphicon-chevron-right btn-lg"></span>
                </li>
            </ul>
            <div id="dv_djmx" style="">
                <table id="gridDJMXInfo"></table>
            </div>
        </div>
        <div class="showbotton">
            <span class="glyphicon glyphicon-chevron-left btn-lg"></span>
        </div>
        <div class="panel panel-default">
            <input type="button" id="btn_delete" class="btn btn-info" style="width:7%;display:inline-block;float:right;margin-left:20px;margin-top:30px;" value="作废" />
            <input type="button" id="btn_back" class="btn btn-info" style="width:7%;display:inline-block;float:right;margin-left:20px;margin-top:30px;" value="撤回" />
            <input type="button" id="btn_submit" class="btn btn-info" style="width:7%;display:inline-block;float:right;margin-left:20px;margin-top:30px;" value="提交" />
            <input type="button" id="btn_edit" class="btn btn-info" style="width:7%;display:inline-block;float:right;margin-left:20px;margin-top:30px;" value="修改" />
            <input type="button" id="btn_add" class="btn btn-info" style="width:7%;display:inline-block;float:right;margin-left:20px;margin-top:30px;" value="新建" />

        </div>
    </form>
</body>
</html>


<script>
    
    var reportServerHOST = '@(ViewBag.ReportServerHOST)';
    var orgId = '@(ViewBag.OrgId)';
    $(function () {
        initControl();
        gridList();
        //搜索框隐藏显示控制
        $(".hiding,.rows,#dv_search,.toolbar").click(function () {
            TabGridHide();
        });
        $(".showbotton").click(function () {
            $(".showbotton").animate({ width: 'hide' }, "fast", function () {
                $("#TabGrid").animate({ left: '0px' }, "slow");
            });
        });
        TabGridHide();
    })

    //隐藏搜索框并显示箭头
    function TabGridHide() {
        var tabGridwith = $("#TabGrid").width() + 200;
        if ($(".showbotton").is(":hidden")) {
            $("#TabGrid").animate({ left: tabGridwith }, "slow", function () {
                $(".showbotton").animate({ width: 'show' }, "fast");
            });
        }
    }

    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/PrepareMedicine/GetPrepareMedicineReturnGridJson",
            height: $(window).height() - 200,
            postData: { thzt: $("#ksbyzt").val(), kssj: $("#kssj").val(), jssj: $("#jssj").val() },
            colModel: [
                {
                    label: '状态', name: 'thzt', width: 60, align: 'left', formatter: function (cellvalue) {
                        //if (cellvalue == 1) return '保存';
                        //else if (cellvalue == 3) return '提交';
                        //else if (cellvalue == 5) return '撤回';
                        //else if (cellvalue == 7) return '作废';
                        if (cellvalue == 1) return '未申请';
                        else if (cellvalue == 2) return '已申请';
                        else if (cellvalue == 3) return '已确认';
                        else if (cellvalue == 4) return '撤回';
                        else if (cellvalue == 5) return '退回';
                        else if (cellvalue == 6) return '作废';
                        else return '';
                        //return $.enum.getDescByValue("EnumKSBYZT", cellvalue);
                    }
                },
                { label: 'Id', name: 'Id', hidden: true },
                { label: '单据号', name: 'djh' },
                { label: '退回药房', name: 'yfmc', width: 60, align: 'left' },
                { label: '科室', name: 'ksmc', width: 80, align: 'left' },
                { label: '病区', name: 'bqmc', width: 100, align: 'left' },
                { label: '创建人', name: 'CreatorCode', width: 80, align: 'left' },
                {
                    label: '创建时间', name: 'CreateTime', width: 150, align: 'left', formatter: "date",
                    formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' } },
                {
                    label: '提交时间', name: 'tjsj', width: 150, align: 'left', formatter: "date",
                    formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' }
                },
                { label: '退回原因', name: 'thyy', width: 60, align: 'left' },
                {
                    label: "操作", name: "", align: "center", width: 150,
                    formatter: function (cellvalue, options, rowobject) {
                        return '<input type="button" id="btn_rpt" onclick="rptdjmx(\'' + rowobject.Id + '\')" class="btn btn-info" style="width:100px;display:inline-block;"  value="打印单据明细"/>'
                    }
                },
                { label: 'yfbm', name: 'yfbm', hidden: true },
                { label: 'bqbm', name: 'bqbm', hidden: true },

            ],

            multiselect: true,

            pager: "#gridPager",
            sortname: 'CreateTime',
            viewrecords: true,
            ondblClickRow: function (rowid, iRow, iCol, e) {
                btn_selectrow(rowid);
            }
        });
        $("#btn_search").click(function () {
            $('#gridDJMXInfo').jqGrid("clearGridData");
            $gridList.jqGrid('setGridParam', {
                postData: { thzt: $("#ksbyzt").val(), kssj: $("#kssj").val(), jssj: $("#jssj").val() },
            }).trigger('reloadGrid');
        });
    }

    function rptdjmx(byid) {
        window.open(reportServerHOST + "/ReportServer/Pages/ReportViewer.aspx?%2fNewtouch.Report.1%2f科室备药退回单&rs:Command=Render&rc:Stylesheet=MyStyleSheetBlankPage&byId=" + byid + "&orgId=" + orgId, "_blank", "height=500, width=1195,top=100, left=50, toolbar =no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no");
    }

    //http://localhost:17105/ReportServer/Pages/ReportViewer.aspx?%2fNewtouch.Report.1%2f%E7%A7%91%E5%AE%A4%E5%A4%87%E8%8D%AF%E7%94%B3%E8%AF%B7%E5%8D%95&rs:Command=Render&rc:Stylesheet=MyStyleSheetBlankPage&byId=ad4d9559-9e0a-4093-9000-9ee2cb6a26e0&orgId=
    //http://59.80.30.184:16015/ReportServer/Pages/ReportViewer.aspx?%2fNewtouch.Report.1%2f%E7%A7%91%E5%AE%A4%E5%A4%87%E8%8D%AF%E9%80%80%E5%9B%9E%E5%8D%95&rs:Command=Render
    //回车事件
    $('#txt_keyword').keydownEnterEvent(function () {
        $('#btn_search').trigger('click');
    })

    //单击事件
    function btn_selectrow(rowId) {
        $('#gridDJMXInfo').jqGrid("clearGridData");//每次单击，先清空明细 再重新加载所有选中行明细
        $.najax({
            type: "Get",
            url: "/PrepareMedicine/QueryPrepareMedicine",
            data: { djId: $("#gridList").jqGrid('getRowData', rowId).Id },
            dataType: "json",
            success: function (ajaxresp) {
                if (ajaxresp !== null) {
                    $('#gridDJMXInfo').newtouchLocalDataGrid(null, ajaxresp);
                }
                $(".showbotton").click().trigger("click");
            }
        });
    }
    //单据明细
    $('#gridDJMXInfo').newtouchLocalDataGrid({
        height: $(window).height() - 200,
        unwritten: false,
        sortable: false,
        colModel: [
            { label: '药品名称', name: 'ypmc', width: 150, align: 'left' },
            { label: '药品代码', name: 'ypdm', width: 60, align: 'right' },
            { label: '药品类别', name: 'yplb', width: 80, align: 'left' },
            { label: '退数量', name: 'tsl', width: 60, align: 'center' },
            { label: '单位', name: 'dw', width: 90, align: 'right' },
            { label: '规格', name: 'gg', width: 120, align: 'right' },
            { label: '有效期', name: 'yxq', width: 100, align: 'right' },
            //{ label: '有效期', name: 'yxq', width: 70, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' } },
            { label: '生产厂家', name: 'sccj', width: 160, align: 'left' }
        ]
    });

    function initControl() {

    }

    //新建
    $('#btn_add').click(function () {
        $.modalOpen({
            id: "OutAreaForm",
            title: "科室备药退回",
            url: "/NurseManage/PrepareMedicine/PrepareMedicineReturnAdd",
            width: "95%",
            height: "95%",
            callBack: function (iframeId) {
                $.modalClose("OutAreaForm");
                $('#btn_search').trigger('click');
            }
        });
    });

    //修改
    $('#btn_edit').click(function () {
        var selRowIds = $("#gridList").jqGrid('getGridParam', 'selarrrow');
        if (selRowIds == null || selRowIds.length == 0) {
            $.modalAlert("请选择需要修改的单据", 'warning');
            return;
        }
        if (selRowIds.length >= 2) {
            $.modalAlert("只能选择一个单据进行修改", 'warning');
            return;
        }
        var rowData = $("#gridList").jqGrid('getRowData', selRowIds[0]);
        if (rowData.thzt != "未申请") {
            $.modalAlert("选中的单据不是未申请状态，不能修改", 'warning');
            return;
        }
        $.modalOpen({
            id: "OutAreaForm",
            title: "科室备药退回",
            url: "/NurseManage//PrepareMedicine/PrepareMedicineReturnAdd?djId=" + rowData.Id,
            width: "95%",
            height: "95%",
            callBack: function (iframeId) {
                $.modalClose("OutAreaForm");
                $('#btn_search').trigger('click');
            }
        });
    });

    //提交
    $('#btn_submit').click(function () {
        var selRowIds = $("#gridList").jqGrid('getGridParam', 'selarrrow');
        if (selRowIds == null || selRowIds.length == 0) {
            $.modalAlert("请选择需要提交的单据", 'warning');
            return;
        }
        if (selRowIds.length >= 2) {
            $.modalAlert("只能选择一个单据进行提交", 'warning');
            return;
        }
        var rowData = $("#gridList").jqGrid('getRowData', selRowIds[0]);
        //if (rowData.thzt != "保存") {
        //    $.modalAlert("选中的单据不是保存状态，不能提交", 'warning');
        //    return;
        //}
        //var postData = rowData;
        var postData = {
            Id: rowData.Id,
            OrganizeId: orgId,
            djh: rowData.djh,
            thzt: "2",
            yfbm: rowData.yfbm,
            ksbm: rowData.ksbm,
            bqbm: rowData.bqbm,
            thyy: rowData.thyy,
            zt: "1",
            CreatorCode: rowData.CreatorCode,
            CreateTime: rowData.CreateTime

        }
        $.najax({
            type: "POST",
            url: "/PrepareMedicine/UpdatePrepareMedicineReturn",
            data: { entity: postData },
            dataType: "json",
            success: function (res) {
                if (res.message == "成功") {
                    $.modalMsg("单据提交成功！", 'success');
                    $('#gridDJMXInfo').jqGrid("clearGridData");
                    $gridList.jqGrid('setGridParam', {
                        postData: { thzt: $("#ksbyzt").val(), kssj: $("#kssj").val(), jssj: $("#jssj").val() },
                    }).trigger('reloadGrid');
                } else {
                    $.modalMsg("单据提交失败：" + res.message, 'warning');
                }
            }
        });
    });

    //撤回
    $('#btn_back').click(function () {
        var selRowIds = $("#gridList").jqGrid('getGridParam', 'selarrrow');
        if (selRowIds == null || selRowIds.length == 0) {
            $.modalAlert("请选择需要撤回的单据", 'warning');
            return;
        }
        if (selRowIds.length >= 2) {
            $.modalAlert("只能选择一个单据进行撤回", 'warning');
            return;
        }
        var rowData = $("#gridList").jqGrid('getRowData', selRowIds[0]);
        if (rowData.thzt != "已申请") {
            $.modalAlert("选中的单据不是已申请状态，不能撤回", 'warning');
            return;
        }
        $.najax({
            type: "POST",
            url: "/PrepareMedicine/Bydjback",
            data: { Djh: rowData.djh },
            dataType: "json",
            success: function (res) {
                if (res.message == "成功") {
                    $('#btn_search').trigger('click');
                    $.modalAlert("单据撤回成功！", 'success');
                } else {
                    $.modalAlert("单据撤回失败：" + res.message, 'warning');
                }
            }
        });
    });

    //作废
    $('#btn_delete').click(function () {
        var selRowIds = $("#gridList").jqGrid('getGridParam', 'selarrrow');
        if (selRowIds == null || selRowIds.length == 0) {
            $.modalAlert("请选择需要作废的单据", 'warning');
            return;
        }
        if (selRowIds.length >= 2) {
            $.modalAlert("只能选择一个单据进行作废", 'warning');
            return;
        }
        var rowData = $("#gridList").jqGrid('getRowData', selRowIds[0]);
        if (rowData.thzt != "未申请") {
            $.modalAlert("请选择未申请的单据进行作废", 'warning');
            return;
        }
        $.najax({
            type: "POST",
            url: "/PrepareMedicine/Bydjdelete",
            data: { ById: rowData.Id },
            dataType: "json",
            success: function (res) {
                if (res.message == "成功") {
                    $('#btn_search').trigger('click');
                    $.modalAlert("单据作废成功！", 'success');
                } else {
                    $.modalAlert("单据作废成功：" + res.message, 'warning');
                }
            }
        });
    });

</script>
